package com.edt.ichannel.api.controller;

import cc.edt.iceutils2.string.IceStringUtils;
import com.alibaba.fastjson.JSON;

import com.edt.ichannel.api.common.controller.BaseController;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.HttpPostParamsTraffic;
import com.edt.ichannel.api.service.MechanismsAuthService;
import com.edt.ichannel.api.service.TrafficService;
import com.edt.ichannel.utils.ReadUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
@RequestMapping("/traffic")
public class trafficController extends BaseController {

    @Resource
    private TrafficService trafficService;

    @Resource
    private MechanismsAuthService mechanismsAuthService;

    private Logger logger = LogManager.getLogger(trafficController.class);


    /**
     *  充值流量
     * @param request request
     * @throws IOException
     */
    @RequestMapping(value = "exchange",method = RequestMethod.POST)
    @ResponseBody
    public void exchange(HttpServletRequest request) throws IOException {
        ActionResult actionResult = new ActionResult();
        String res = ReadUtil.readRequestBody(request);
        if (IceStringUtils.isBlank(res)) {
            actionResult.setSuccess(false);
            actionResult.setMessage("接收参数为空");
        }
        else {
            actionResult = trafficService.exchange(res);
        }
        WriterToPageByJson(actionResult);

    }

    /**
     * 查询充值详细
     * @param request request
     * @throws IOException
     */
    @RequestMapping(value = "detail",method = RequestMethod.POST)
    public void detail(HttpServletRequest request) throws IOException {
        ActionResult actionResult = new ActionResult();
        String res = ReadUtil.readRequestBody(request);
        if (IceStringUtils.isBlank(res)) {
            actionResult.setSuccess(false);
            actionResult.setMessage("接收参数为空");
        }
        else {
            HttpPostParamsTraffic params = JSON.parseObject(res, HttpPostParamsTraffic.class);
            actionResult = validateDetailParam(params);
            if (actionResult.isSuccess()) {
                actionResult = trafficService.getTraffiicByMechanismsIdAndPhone(params.getMechanismsId(),params.getPhone());
            }
        }
        WriterToPageByJson(actionResult);

    }



    private ActionResult validateDetailParam(HttpPostParamsTraffic params) {
        ActionResult result = new ActionResult();
        if(params == null){
            result.setSuccess(false);
            result.setMessage("接收参数为空");
            return result;
        }
        if(params.getMechanismsId() == null || "".equals(params.getMechanismsId())){
            result.setSuccess(false);
            result.setMessage("mechanismsid 节点为空，请核对");
            return result;
        }
        if(params.getPhone() == null || "".equals(params.getPhone())){
            result.setSuccess(false);
            result.setMessage("phone 节点为空，请核对");
            return result;
        }
        result.setSuccess(true);
        result.setMessage("验证通过");
        return result;
    }
}
